<template>
  <div>
    <header class="header">
      <div class="header-title">
        <img @click="scanCode" class="saoma" src="@/assets/home/saoma.png" alt="" />
      </div>
      <div class="header-actions">
        <!-- <div class="header-avatar">
        <img src="@/assets/home/avatar.png" alt="avatar" />
      </div> -->
        <span>{{ userStore.userInfo.name }}</span>
        <div class="header-grid" @click="showDrawer = true">
          <van-icon name="apps-o" size="36" color="#fff" />
          <span class="header-badge">21</span>
        </div>
        <van-popup
          v-model:show="showDrawer"
          position="right"
          :style="{ width: '40%', height: '100%' }"
          :overlay="true"
          :close-on-click-overlay="true"
          class="drawer-popup"
        >
          <div class="drawer-content">
            <div class="drawer-top">
              <img class="drawer-avatar" src="@/assets/home/avatar.png" alt="avatar" />
              <div class="drawer-username">{{ userStore.userInfo.name || '超级管理员' }}</div>
            </div>
            <div class="drawer-menu">
              <div
                v-for="item in menuItems"
                :key="item.id"
                :class="['drawer-menu-item', { active: activeMenu === item.id }]"
                @click="selectMenu(item.id)"
              >
                <span>{{ item.title }}</span>
                <template v-if="item.badge">
                  <span class="menu-badge">{{ item.badge }}</span>
                </template>
              </div>
              <div @click="outlog" class="logout">退出登录</div>
            </div>
          </div>
        </van-popup>
      </div>
    </header>
    <div class="content-area">
      <slot></slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useUserStore } from '@/stores/user'
import { showConfirmDialog } from 'vant'
import { useRouter } from 'vue-router'
const userStore = useUserStore()
const showDrawer = ref(false)
const activeMenu = ref(1)

const router = useRouter()

const menuItems = [
  { id: 1, title: '数据看板', path: '/' },
  { id: 2, title: '资产管理', path: '/asset' },
  { id: 3, title: '机房管理', path: '/room' },
  { id: 4, title: '网关管理', path: '/gateway' },
  { id: 5, title: '消息通知', badge: 2, path: '/message' },
  { id: 6, title: '资产地图', path: '/map' },
]

function selectMenu(id: number) {
  activeMenu.value = id
  let path = menuItems.find((item) => item.id === id)?.path || '/'

  router.push(path)
  showDrawer.value = false
}
function scanCode() {
  router.push('/scan')
}
function outlog() {
  showConfirmDialog({
    title: '确认退出',
    message: '确定要退出登录吗？',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  })
    .then(() => {
      localStorage.removeItem('token')
      userStore.setUserInfo({ name: '', role: '', id: '' })
      router.push('/login')
    })
    .catch(() => {})
}
</script>

<style scoped>
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  /* background: linear-gradient(90deg, #3388ff 0%, #4faaff 100%); */
  padding: 0 20px;
  height: 60px;
  color: #fff;
}
.saoma {
  width: 50px;
  height: 50px;
}
.header-title {
  font-size: 1rem;
  font-weight: bold;
  letter-spacing: 2px;
}

.content-area {
  height: calc(100vh - 60px);
  overflow: auto;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 32px;
}

.header-avatar {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.header-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.header-grid {
  position: relative;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.header-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  background: #ff5b5b;
  color: #fff;
  font-size: 10px;
  border-radius: 50%;
  min-width: 20px;
  text-align: center;
  font-weight: bold;
  border: 2px solid #fff;
  box-shadow: 0 0 0 2px #3388ff;
}

.drawer-popup >>> .van-popup {
  background: #222;
}

.drawer-content {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: #222;
  padding: 0 10px;
}

.drawer-top {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 32px 0 24px 0;
}

.drawer-avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: #fff;
  margin-bottom: 12px;
  object-fit: cover;
}

.drawer-username {
  color: #fff;
  font-size: 16px;
  font-weight: bold;
}

.drawer-menu {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 32px;
}

.drawer-menu-item {
  color: #fff;
  font-size: 14px;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: center;
  transition:
    background 0.2s,
    color 0.2s;
}

.drawer-menu-item.active {
  background: #176bff;
  color: #fff;
}

.menu-badge {
  background: #ff3b30;
  color: #fff;
  font-size: 12px;
  border-radius: 50%;
  min-width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 8px;
  padding: 0 6px;
}
.logout {
  padding: 5px 6px;
  border-radius: 8px;
  font-size: 14px;
  border: 1px solid #ffffff;
  margin-top: 50px;
  text-align: center;
}
</style>
